home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / Documents / NeXTAnswers / appkit.651 < prev    next >
Text File  |  1992-02-06  |  4KB  |  73 lines

  1. {\rtf0\ansi{\fonttbl\f2\fnil Times-Roman;\f0\fmodern Courier;}
  2. \paperw13040
  3. \paperh10800
  4. \margl120
  5. \margr120
  6. {\colortbl\red0\green0\blue0;}
  7. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f2\b0\i0\ul0\fs28 AppKit Timer Events\
  8. \
  9. Q:  What are timer events useful for?  How are they different from timed entries?\
  10. \
  11. A: These two different event-handling features are often confused because of the similarities of their names.  However, they are intended for different uses.  A timed entry is used for scheduling 
  12. \b regular
  13. \b0  periodic activities in your application.  See NextAnswers appkit.650 for more information about timed entries.  A timer event is used in conjunction with a modal loop when an application must continue to do something even when no user events are being received.  Modal loops are used to 
  14. \b temporarily
  15. \b0  circumvent the primary application loop.  The loop is triggered by an event such as a mouse down event, and is terminated when a specific event is encountered, such as a mouse up event.\
  16. \
  17. The scroll buttons in the standard NeXT Scroller use a timer event in a modal loop to scroll continuously while the mouse button is held down.  When the Scroller receives a mouse down event on a scroll button, it begins to scroll the contents of the view.   While the user is simply holding down the mouse button, no events are generated.  The application must continue to scroll the view, even though the events have stopped.  This is when timer events come into play.  Once you start a timer, it will insert timer events into the queue at regular time intervals.  These events are “dummy” events —the user has not actually done something (moved the mouse, hit a key, let the mouse up, etc) but the event indicates that a certain time interval has passed since the last event.  Turn on the timer as the modal loop begins, and turns it off when the modal loop terminates.  (If you forget to turn it off, you may suffer performance problems because of the extra event processing!)   During the execution of the modal loop, you call 
  18. \b getNextEvent
  19. \b0 : with the appropriate event mask to receive timer events as they are generated and do the desired processing for each one.   Here is an example of a modal loop which implements the scrolling behavior described above:\
  20. \
  21.  
  22. \f0\fs24 - mouseDown:(NXEvent *) thisEvent\
  23. \{\
  24.     int            shouldLoop = YES;\
  25.     int            oldMask;\
  26.     NXTrackingTimer    myTimer;\
  27.     NXEvent        *nextEvent, lastEvent;\
  28.     \
  29.     oldMask = [window addToEventMask:NX_LMOUSEDRAGGEDMASK];\
  30.     lastEvent = thisEvent;\
  31.     NXBeginTimer(&myTimer, 0.05, 0.05);\
  32.     \
  33.     while (shouldLoop) \{\
  34.         nextEvent = [NXApp getNextEvent:(NX_LMOUSEUPMASK\
  35.                             | NX_LMOUSEDRAGGEDMASK\
  36.                             | NX_TIMERMASK)];\
  37.         switch (nextEvent->type) \{\
  38.         case NX_LMOUSEUP:\
  39.             shouldLoop = NO;\
  40.             break;\
  41.         case NX_LMOUSEDRAGGED:\
  42.             lastEvent = *nextEvent;\
  43.             break;\
  44.         case NX_TIMER:\
  45.             [self autoscroll:&lastEvent];\
  46.             break;\
  47.         default:\
  48.             break;\
  49.         \}\
  50.     \}\
  51.     \
  52.     NXEndTimer(&myTimer);\
  53.     [window setEventMask:oldMask];\
  54.     return self;\
  55. \}\
  56.  
  57. \f2\fs28 \
  58. The code segment above was taken directly from the Concepts manual of the NeXT System Reference Manual.  For more complete information about modal loops and timer events, please read Concepts Chapter 7 pages 58 - 70.\
  59. \
  60. In 
  61. \b /NextDeveloper/Examples
  62. \b0  there are two example programs (
  63. \b ScrollDoodScroll
  64. \b0  and 
  65. \b Draw
  66. \b0 ) which show how to use timer events with modal loops 
  67. \pard\tx620\tx1240\tx1860\tx2480\tx3100\tx3720\tx4340\tx4980\tx5600\tx6220\fc0 and give you an impression as to why they are useful.  
  68. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600 \
  69. \
  70. QA651\
  71. \
  72.  
  73.